package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.LoginActivity;
import com.hongxun.design.activitys.RegeditActivity;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.umeng.message.UTrack;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/5/19.
 */
public class UserInfoSetAdapter extends BaseAdapter {
    List<SystemTypeBean> itemBean;
    Context context;
    LayoutInflater inflater;

    public UserInfoSetAdapter(List<SystemTypeBean> itemBean, Context context) {
        this.itemBean = itemBean;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBean.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.parseInt(itemBean.get(position).code);
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        SystemTypeBean bean = (SystemTypeBean) getItem(i);
        int viewtype = getItemViewType(i);
        Holder holder = null;
        OutHolder outHolder = null;
        if (view == null) {
            if (viewtype == Constant.onlyOut) {
                view = inflater.inflate(R.layout.listview_item_outlogin, null);
                outHolder = new OutHolder(view);
                view.setTag(outHolder);
            } else {
                view = inflater.inflate(R.layout.listview_item_userinfo, null);
                holder = new Holder(view);
                view.setTag(holder);
            }

        } else {
            if (viewtype == Constant.onlyOut) {
                outHolder = (OutHolder) view.getTag();
            } else {
                holder = (Holder) view.getTag();
            }
        }
        if (viewtype == Constant.onlyOut) {
            outHolder.outlogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (MyApp.NetWorkConnect&&MyApp.isOnLine) {

                        try {
                            MyApp.mPushAgent.removeAlias(MyApp.userInfo.getUser_id(), MyApp.PushType, new UTrack.ICallBack() {
                                @Override
                                public void onMessage(boolean isSuccess, String message) {
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        MyApp.userInfo.clearBean();//清理个人信息
                        MyApp.currProject.clearBean();//清理项目
                        MyApp.activity = null;
                        MyApp.ISOUTLOGIN = true;
                        MyApp.iscreate = true;
                        if (MyApp.HomeActivity != null) MyApp.HomeActivity.closeService();
                        IntentToActivity.intent((Activity) context, LoginActivity.class);
                        ((Activity) context).finish();
                        // if (MyApp.HomeActivity != null) MyApp.HomeActivity.finish();

                    }else if (!MyApp.isOnLine){
                        ToastShow.showShort(context,"离线模式下无法退出登录！");
                    }else{
                        ToastShow.showShort(context,"网络异常，注销失败！");
                    }
                }
            });


        } else {
            holder.textView.setText(bean.getTypename());
            if (viewtype == Constant.onlyRead) {
                if (bean.getTypecode() == null || bean.getTypecode().length() == 0) {
                    holder.right.setText("无");
                } else {
                    holder.right.setText(bean.getTypecode());
                }
                holder.right.setTextColor(context.getResources().getColor(R.color.bar_left));
                holder.right.setTextSize(14);
            } else {
                SetTypeface.set(holder.right, IconSourse.RIGHTARROW);
                holder.right.setTextSize(18);
                holder.right.setTextColor(context.getResources().getColor(R.color.bar_right));
            }
            holder.textView.setOnClickListener(new MyOnclickListener(bean) {
                @Override
                public void onClick(View view, SystemTypeBean bean) {
                    Bundle bundle = new Bundle();
                    if (bean.code.equals(Constant.onlyActivity + "")) {
                        if (bean.getTypename().equals(MString.Password)) {
                            bundle.putString(MString.TYPE, MString.ModifyPassword);
                            IntentToActivity.intent((Activity) context, RegeditActivity.class, bundle);
                        } else if (bean.getTypename().equals(MString.SecretSet)) {
                            bundle.putString(MString.TYPE, MString.SecretSet);
                            IntentToActivity.intent((Activity) context, RegeditActivity.class, bundle);
                        } else if (bean.getTypename().equals(MString.PInformationSet)) {
                            bundle.putString(MString.TYPE, MString.PInformationSet);
                            IntentToActivity.intent((Activity) context, RegeditActivity.class, bundle);
                        }
                    }
                }
            });
        }
        return view;
    }

    abstract class MyOnclickListener implements View.OnClickListener {
        SystemTypeBean bean;

        MyOnclickListener(SystemTypeBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            onClick(view, bean);
        }

        public abstract void onClick(View view, SystemTypeBean bean);
    }

    class Holder {
        TextView textView, right;

        Holder(View view) {
            textView = (TextView) view.findViewById(R.id.userinfo_name);
            right = (TextView) view.findViewById(R.id.userinfo_right);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }

    class OutHolder {
        TextView outlogin;

        OutHolder(View view) {
            outlogin = (TextView) view.findViewById(R.id.outlogin);
            SetTypeface.set(outlogin, IconSourse.cancellation);
            outlogin.append(" 退出登录");
        }
    }
}
